<?php
/*
 * functions.php
 * 
 * Copyright 2013  <>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

/*
 *
 * name: depositIsValid
 *
 *              Check if the input parameters for insert/update deposit are valid
 *
 * @param:
 *              $material: material of the deposit
 *              $size: size of the deposit
 *              $x_coord (optional): x coord of the deposit
 *              $y_coord (optional): y coord of the deposit
 *              $planet_size (optional): size of the deposit's planet
 * 
 *
 * @return:
 *              true: input parameters valid
 *              false: no valid
 */
function depositIsValid($material, $size, $x_coord = FALSE, $y_coord = FALSE, $planet_size = FALSE) {
	
	global $rm_array;
	
	settype($size, "integer");
	settype($material, "string");
	
	//check that the material is valid
	if(!in_array($material, $rm_array, TRUE)){
		
		echo "<p align=\"center\"><strong>Error: Material " .$material." does not exists</strong></p>";
		return false;
	}
   
	//check that the size is valid (numeric, >0)
	if($size <= 0){
		
		echo "<p align=\"center\"><strong>Error: Incorrect deposit size: " .$size."</strong></p>";
		return false;
	}
   
	//check that the coords of the new deposit is valid (inside planet bounds) if set
	if ($x_coord !== FALSE AND $y_coord !== FALSE AND $planet_size !== FALSE) {
		
		settype($x_coord, "integer");
		settype($y_coord, "integer");
		settype($planet_size, "integer");
		
		if($x_coord < 0 or $y_coord < 0 or $x_coord >= $planet_size or $y_coord >= $planet_size) {
			
			echo "<p align=\"center\"><strong>Error: Incorrect deposit coords: " .$x_coord." , ".$y_coord."</strong></p>";
			return false;
		}
	}
	
	return true;
}

/*
 *
 * name: planetIsValid
 *
 *              Check if the input parameters for insert a planet are valid
 *
 * @param:
 *              $size: size of the planet
 * 
 *
 * @return:
 *              true: input parameters valid
 *              false: no valid
 */
function planetIsValid($size) {
	
	settype($size, "integer");
   
	//check planet size
	if($size <= 0 or $size > MAX_PLANET_SIZE) {
		
		echo "<p align=\"center\"><strong>Error: Incorrect planet size: " .$size."</strong></p>";
		return false;
	}
	
	return true;
	
}
?>

